package view;

import controller.ManageClickListener;
import pojo.Manage;
import service.ManageService;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.List;

public class ManageView extends JPanel {
    private ManageClickListener manageClickListener;
    private List<Manage> manageList;
    private JScrollPane scroll;
    private JLabel labelSelf;
    public ManageView(List<Manage> manageList){
        manageClickListener=new ManageClickListener(this);
        this.manageList = manageList;
        this.setLayout(new BorderLayout());
        this.add(this.getLabel(),BorderLayout.NORTH);
        this.add(this.getPanel(),BorderLayout.CENTER);
    }
    //刷新
    public void refresh(){
        ManageService service = new ManageService();
        manageList = service.searchAll();
        this.remove(scroll);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }

    public JPanel getLabel(){
        JPanel panel=new JPanel();
        labelSelf=new JLabel("所有借阅图书");
        labelSelf.setFont(( new Font( "隶书" , Font.BOLD , 30)));
        panel.add(labelSelf);
        return panel;
    }
    public JScrollPane getPanel(){
        String[] titles={"书号","学号", "租借日期", "归还日期"};
        Object data[][] = new Object[manageList.size()][4];
        Iterator<Manage> iter = manageList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Manage manage= iter.next();
            data[i][0] = manage.getBookId();
            data[i][1] = manage.getUserId();
            data[i][2] = manage.getLendTime();
            data[i][3] = manage.getReturnTime();
            i++;
        }
        JTable table = new JTable(data,titles);
        table.addMouseListener(manageClickListener);
        manageClickListener.setTable(table);
        scroll = new JScrollPane(table);
        return scroll;
    }
}
